package com.bzm;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description:mybatis-plus 代码生成器
 * @author hemiao
 * @time:2019年5月12日 下午9:07:48
 */
public class MybatisGenerator {

	/**
	 * 数据库Driver
	 */
	public static final String driver_Name = "com.mysql.cj.jdbc.Driver";

	/**
	 * 数据库地址
	 */
	//public static final String url = "jdbc:mysql://localhost:3306/bzm_2020_db";
	public static final String url = "jdbc:mysql://192.168.10.33:3306/statistics";
	public static final String dbUrl = url+"?serverTimezone=UTC&useSSL=false&allowPublicKeyRetrieval=true";

	/**
	 * 数据库账号
	 */
	public static final String username = "root";
	public static final String db_username = username;
	
	/**
	 * 数据库密码
	 */
	//public static final String password = "123456";
	public static final String password = "mysql_OA0!";
	public static final String db_password = password;

	/**
	 * 模块名称
	 */
	public static final String moduleName = "";

	/**
	 * 生成的表Include
	 */	
	public static final String[] Include = new String[] {"tbl_event_info"};
	
	public static void main(String[] args) {
        GlobalConfig config = new GlobalConfig();
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setDbType(DbType.MYSQL)
                .setUrl(dbUrl)
                .setUsername(db_username)
                .setPassword(db_password)
                .setDriverName(driver_Name);
        
        PackageConfig pc = new PackageConfig()
		.setParent("com.jmyd.scene")
		//.setModuleName("category")	
        //.setController("controller."+moduleName)
        .setEntity("entity")
        .setMapper("dao");
        //.setServiceImpl("service."+moduleName+".impl")
        //.setService("service."+moduleName);
        
        StrategyConfig strategyConfig = new StrategyConfig();
        strategyConfig
                .setCapitalMode(true)
                //这里结合了Lombok，所以设置为true，如果没有集成Lombok，可以设置为false
                .setEntityLombokModel(false)
                .setInclude(Include)
                .setNaming(NamingStrategy.underline_to_camel);
        //这里因为我是多模块项目，所以需要加上子模块的名称，以便直接生成到该目录下，如果是单模块项目，可以将后面的去掉
        String projectPath = System.getProperty("user.dir");
        // 自定义配置
        InjectionConfig cfg = new InjectionConfig() {
            @Override
            public void initMap() {
            	
            	
            }
        };
        // 如果模板引擎是 freemarker
        String templatePath = "/templates/mapper.xml.ftl";
        // 如果模板引擎是 velocity	
        // String templatePath = "/templates/mapper.xml.vm";

        // 自定义输出配置
        List<FileOutConfig> focList = new ArrayList<>();
        // 自定义配置会被优先输出
        focList.add(new FileOutConfig(templatePath) {
            @Override
            public String outputFile(TableInfo tableInfo) {
            	// 自定义输出文件名	
            	String outputFile = projectPath + "/src/main/java/" + pc.getParent()+"." + pc.getMapper()
                + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
            	System.out.println("################outputFile = "+outputFile);	
                return outputFile;
            }
        });

        cfg.setFileOutConfigList(focList);
        //设置作者，输出路径，是否重写等属性
        config.setActiveRecord(false)
                .setEnableCache(false)
                .setAuthor("hemiao")
                .setOutputDir(projectPath + "/src/main/java")
                .setFileOverride(true)		// 是否覆盖同名文件，默认是false
                .setBaseResultMap(true)		// XML ResultMap
                .setBaseColumnList(false)	// XML columList
                .setServiceName("%sService");
        new AutoGenerator()
                .setGlobalConfig(config)
                .setDataSource(dataSourceConfig)
                .setStrategy(strategyConfig)
                .setTemplateEngine(new FreemarkerTemplateEngine())
                .setCfg(cfg)
                //这里进行包名的设置	
                .setPackageInfo(pc).execute();
    }
	
}
